#!/usr/bin/env python
# -*- coding:utf8 -*-
# uncompyle6 version 2.15.1
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.10 (default, Jul  1 2017, 13:36:56) 
# [GCC 4.4.6 20110731 (Red Hat 4.4.6-4)]
# Embedded file name: ./esb/components/bk/apis/cc/update_host_info.py
# Compiled at: 2017-11-16 15:44:28
"""
Copyright © 2012-2017 Tencent BlueKing. All Rights Reserved. 蓝鲸智云 版权所有
"""
import json
from django import forms
from common.forms import BaseComponentForm, TypeCheckField
from common.constants import API_TYPE_OP
from components.component import Component
from .toolkit import tools, configs

class UpdateHostInfo(Component):
    """
    apiLabel 更新主机属性
    apiMethod POST
    
    ### 功能描述
    
    更新主机属性
    
    ### 请求参数
    
    {{ common_args_desc }}
    
    #### 接口参数
    
    | 字段      |  类型      | 必选   |  描述      |
    |-----------|------------|--------|------------|
    | app_id    |  int       | 是     | 业务ID |
    | host_id   |  int       | 是     | 主机ID |
    | std_property |  dict   | 否     | 标准属性数据, 数组格式；允许修改的标准属性：HostName, BakOperator, Operator, Description, Source, OSName, DeviceClass, Mem, Cpu, osType |
    | cus_property |  dict   | 否     | 自定义属性 |
    
    ### 请求参数示例
    
    ```python
    {
        "app_code": "esb_test",
        "app_secret": "xxx",
        "bk_token": "xxx",
        "app_id": 1,
        "host_id": 12345,
        "std_property": {
            "HostName": "hostname",
            "OSName": "linux",
            "Cpu": 4,
        }
    }
    ```
    
    ### 返回结果示例
    
    ```python
    {
        "result": true,
        "code": "00",
        "data": null,
        "message": ""
    }
    ```
    """
    sys_name = configs.SYSTEM_NAME
    api_type = API_TYPE_OP
    host = configs.host

    class Form(BaseComponentForm):
        app_id = forms.IntegerField(label=u'业务ID', required=True)
        host_id = forms.IntegerField(label=u'主机ID', required=True)
        std_property = TypeCheckField(label=u'标准属性数据', promise_type=dict, required=False)
        cus_property = TypeCheckField(label=u'自定义属性', promise_type=dict, required=False)

        def clean(self):
            data = self.cleaned_data
            return {'ApplicationID': data['app_id'],
               'HostID': data['host_id'],
               'stdProperty': json.dumps(data['std_property']),
               'cusProperty': json.dumps(data['cus_property'])
               }

    def handle(self):
        client = tools.CCClient(self.outgoing.http_client)
        self.response.payload = client.post_request(self.host, '/api/host/updateHostInfo', data=self.form_data)
